package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.Shop;
import com.hualan.dao.MenDianDao;
import com.hualan.dao.MenDianDaoImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/select")
public class PanghuSelectServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
//        接收数据 响应数据
        String page = req.getParameter("page");
        String size = req.getParameter("size");
        String shopPhone = req.getParameter("phone");
        String shopName = req.getParameter("shopName");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        System.out.println("后端获取的时间:" + startTime + ", " + endTime);

        // 处理分页查询
        if (page == null || "".equals(page)){
            page = "1";
        }
        if (size == null || "".equals(size)){
            size = "10";
        }
        MenDianDao menDianDao = new MenDianDaoImpl();
        List<Shop> shopList = menDianDao.searchMenDian(shopName,shopPhone,startTime,endTime);
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",shopList);
        if ( shopList != null){
            map.put("code",200);
            map.put("message","请求成功");
        }else {
            map.put("code",300);
            map.put("message","获取数据失败");
        }
        String jsonString = JSON.toJSONString(map);
        resp.getWriter().write(jsonString);
//        // 获取查询总条数
//        Long total = Long.valueOf(menDianDao.size());
//
//        System.out.println("long类型total转成int" + total.intValue());
//
//        PageResult pageResult = new PageResult(Integer.valueOf(pageNum), Integer.valueOf(pageSize), (int) Math.ceil(total.intValue() / Integer.valueOf(pageSize)), total.intValue(), courierStationList);
//
//        // 查询结果
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setData(pageResult);
//        jsonResult.setStatus(200);
//
//        // 处理数据转化为json
//        String jsonString = JSON.toJSONString(jsonResult);
//        System.out.println("这是转换后要传给前端的json文件" + jsonString);
//


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }
}
